package com.ahjz.ahjzdemo;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendForm;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.components.YAxis.AxisDependency;
import com.github.mikephil.charting.components.YAxis.YAxisLabelPosition;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.MPPointF;

public class DetailActivity extends Activity implements
		OnSeekBarChangeListener, OnChartValueSelectedListener {
	protected BarChart mChart;

	private float[] dataList = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);

		loadView();
		loadData();
	}

	private void loadData() {
		Intent intent = this.getIntent();
		dataList = intent.getFloatArrayExtra("datalist");

		// dataList = new float[5];
		// dataList[0] = 0;
		// dataList[1] = 1;
		// dataList[2] = 2;
		// dataList[3] = 3;
		// dataList[4] = 4;

		float start = 0f;
		mChart.getXAxis().setAxisMinValue(start);
		mChart.getXAxis().setAxisMaxValue(start + dataList.length + 2);

		ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();

		for (int i = 0; i < dataList.length; i++) {
			yVals1.add(new BarEntry(i + 1f, dataList[i]));
		}

		BarDataSet set1;

		if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
			set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
			set1.setValues(yVals1);
			mChart.getData().notifyDataChanged();
			mChart.notifyDataSetChanged();
		} else {
			set1 = new BarDataSet(yVals1, "The year 2017");
			// set1.setColors(ColorTemplate.MATERIAL_COLORS);

			ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
			dataSets.add(set1);

			BarData data = new BarData(dataSets);
			data.setValueTextSize(10f);
			data.setBarWidth(0.9f);

			mChart.setData(data);
		}
	}

	private void loadView() {
		mChart = (BarChart) findViewById(R.id.barchart);
		mChart.setOnChartValueSelectedListener(this);

		mChart.setDrawBarShadow(false);
		mChart.setDrawValueAboveBar(true);

		mChart.setDescription("");

		// if more than 60 entries are displayed in the chart, no values will be
		// drawn
		mChart.setMaxVisibleValueCount(60);

		// scaling can now only be done on x- and y-axis separately
		mChart.setPinchZoom(false);

		mChart.setDrawGridBackground(false);
		// mChart.setDrawYLabels(false);

		XAxis xAxis = mChart.getXAxis();
		xAxis.setPosition(XAxisPosition.BOTTOM);
		xAxis.setDrawGridLines(false);
		xAxis.setGranularity(1f); // only intervals of 1 day
		xAxis.setLabelCount(7);

		YAxis leftAxis = mChart.getAxisLeft();
		leftAxis.setLabelCount(8, false);
		leftAxis.setPosition(YAxisLabelPosition.OUTSIDE_CHART);
		leftAxis.setSpaceTop(15f);
		leftAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true)

		YAxis rightAxis = mChart.getAxisRight();
		rightAxis.setDrawGridLines(false);
		rightAxis.setLabelCount(8, false);
		rightAxis.setSpaceTop(15f);
		rightAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true)

		Legend l = mChart.getLegend();
		l.setPosition(LegendPosition.BELOW_CHART_LEFT);
		l.setForm(LegendForm.SQUARE);
		l.setFormSize(9f);
		l.setTextSize(11f);
		l.setXEntrySpace(4f);

		// l.setCustom(ColorTemplate.VORDIPLOM_COLORS, new String[] { "abc",
		// "def", "ghj", "ikl", "mno" });

		// mChart.setMarkerView(new XYMarkerView(this, xAxisFormatter));
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		mChart.invalidate();
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
	}

	protected RectF mOnValueSelectedRectF = new RectF();

	@SuppressLint("NewApi")
	@Override
	public void onValueSelected(Entry e, Highlight h) {

		if (e == null)
			return;

		RectF bounds = mOnValueSelectedRectF;
		mChart.getBarBounds((BarEntry) e, bounds);
		MPPointF position = mChart.getPosition(e, AxisDependency.LEFT);

		Log.i("bounds", bounds.toString());
		Log.i("position", position.toString());

		Log.i("x-index", "low: " + mChart.getLowestVisibleX() + ", high: "
				+ mChart.getHighestVisibleX());

		MPPointF.recycleInstance(position);
	}

	@Override
	public void onNothingSelected() {
	}
}
